package com.kobe.algorithm.leetcode.case739;

import java.util.Stack;

/**
 * @author ：caopengfei
 * @date ：2021/2/24
 * i = 0 73
 * i = 1 73<74   pop73 push 74
 * i = 2 74<75   peek74 push75
 * i = 4 71<75 push 71
 * i = 5  69<71 push 69
 * i =6 72>69  pop 69  1   72>71 pop 71 2
 * i = 7
 */
public class Solution {

    public int[] dailyTemperatures(int[] T) {
        int [] result = new int[T.length];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < T.length; i++) {
            if(stack.empty() || i == 0){
                stack.push(i);
            }else{
                 int n = stack.peek();
                 while(T[n] < T[i]){
                     result[n] = i-n;
                     stack.pop();
                     if(stack.empty()){
                         break;
                     }
                     n = stack.peek();
                 }
                stack.push(i);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] temperatures = {73, 74, 75, 71, 69, 72, 76, 73};
        solution.dailyTemperatures(temperatures);
    }

}
